package top.fzqblog.kotlinapp.activity

import android.content.Intent
import android.os.Bundle
import android.os.SystemClock
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.view.ViewPager
import android.support.v7.app.AppCompatActivity
import top.fzqblog.kotlinapp.R
import top.fzqblog.kotlinapp.app.KotlinApp
import kotlinx.android.synthetic.main.activity_main.*;
import rx.functions.Action1
import top.fzqblog.kotlinapp.fragment.*
import top.fzqblog.kotlinapp.func.log
import top.fzqblog.kotlinapp.func.startActivity
import top.fzqblog.kotlinapp.util.getUnionId
import java.util.ArrayList

/**
 * Created by 抽离 on 2017/11/7 0007.
 */
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
//        toast("啦啦啦")

        bnve.enableAnimation(false)
        bnve.enableShiftingMode(false)
        bnve.enableItemShiftingMode(false)

        val fragments = ArrayList<Fragment>()

        with(fragments){
            add(Sell())
            add(Rent())
            add(WorkBench())
            add(Buy())
            add(FindRent())
        }




        vp.adapter = VpAdapter(supportFragmentManager, fragments)


        bnve.setupWithViewPager(vp)//相当于实现了vp.addOnPageChangeListener()和setOnNavigationItemSelectedListener


        vp.addOnPageChangeListener(object: ViewPager.OnPageChangeListener{
            override fun onPageScrollStateChanged(position: Int) {

            }

            override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {

            }

            override fun onPageSelected(position: Int) {
//                if(position == 2){
//                    startActivity(Intent(this@MainActivity, LoginActivity::class.java))
//                }
            }

        })


        log(KotlinApp.instance.packageName)

//        val result = getResult();
//        toast(result)

        /**
         * 异步任务anko写法
         */
//        doAsync {
//            val result = getResult()
//            uiThread {
//                toast(result)
//            }
//        }

    }

    private fun getResult(): String{
        SystemClock.sleep(1000 * 10)//模拟网络延迟
        return "2"
    }

    override fun onSaveInstanceState(outState: Bundle?) {
        super.onSaveInstanceState(outState)
    }

    override fun onBackPressed() {
//        super.onBackPressed()
    }

}

/**
 * 直接可以在类上面加到
 */
private class VpAdapter(fm: FragmentManager?, var data: List<Fragment>): FragmentPagerAdapter(fm){

    override fun getItem(position: Int): Fragment {
        return data[position]
    }

    override fun getCount(): Int {
        return data.size
    }

}